﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using System.ComponentModel.Design;
using centraldba.msx_config_addin.Events;

namespace centraldba.msx_config_addin
{
    /// <summary>
    /// This class implements the tool window exposed by this package and hosts a user control.
    ///
    /// In Visual Studio tool windows are composed of a frame (implemented by the shell) and a pane, 
    /// usually implemented by the package implementer.
    ///
    /// This class derives from the ToolWindowPane class provided from the MPF in order to use its 
    /// implementation of the IVsUIElementPane interface.
    /// </summary>
    [Guid("c4a4a205-895c-4946-97e8-268f04a6bead")]
    public class twMSXConfiguration : ToolWindowPane
    {
        MenuCommand cmdAdd, cmdDelete, cmdRefresh;

        /// <summary>
        /// Standard constructor for the tool window.
        /// </summary>
        public twMSXConfiguration() : base(null)
        {
            // Set the window title reading it from the resources.
            this.Caption = Resources.ToolWindowTitle;

            // Set the image that will appear on the tab of the window frame
            // when docked with an other window
            // The resource ID correspond to the one defined in the resx file
            // while the Index is the offset in the bitmap strip. Each image in
            // the strip being 16x16.
            this.BitmapResourceID = 401;
            this.BitmapIndex = 0;

            // Create the toolbar. 
            this.ToolBar = new CommandID(GuidList.guidTWmsxConfigureToolbarCmdSet, PkgCmdIDList.TWmsxConfigureToolbar);
            this.ToolBarLocation = (int)VSTWT_LOCATION.VSTWT_TOP;

            // Create the handlers for the toolbar commands. 
            var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (null != mcs)
            {
                var toolbarbtnAddCmdID = new CommandID(GuidList.guidTWmsxConfigureToolbarCmdSet, PkgCmdIDList.cmdidTWmsxAddCmd);
                var menuAddItem = new MenuCommand(new EventHandler(cmdAdd_Click), toolbarbtnAddCmdID);
                mcs.AddCommand(menuAddItem);
                cmdAdd = menuAddItem;

                // Command for the combo itself 
                var toolbarbtnDeleteCmdID = new CommandID(GuidList.guidTWmsxConfigureToolbarCmdSet, PkgCmdIDList.cmdidTWmsxDeleteCmd);
                var menuDeleteItem = new MenuCommand(new EventHandler(cmdDelete_Click), toolbarbtnDeleteCmdID);
                menuDeleteItem.Enabled = false;
                mcs.AddCommand(menuDeleteItem);
                cmdDelete = menuDeleteItem;

                // Command for the combo's list 
                var toolbarbtnRefreshCmdID = new CommandID(GuidList.guidTWmsxConfigureToolbarCmdSet, PkgCmdIDList.cmdidTWmsxRefreshCmd);
                var menuRefreshItem = new MenuCommand(new EventHandler(cmdRefresh_Click), toolbarbtnRefreshCmdID);
                mcs.AddCommand(menuRefreshItem);
                cmdRefresh = menuRefreshItem;
            } 

            // This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
            // we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on 
            // the object returned by the Content property.
            msx_list dbToolWindow = new msx_list(this);
            dbToolWindow.MSXServerSelected += new EventHandler<SelectedServerEventArgs<centraldba_library.Admin.msx_server>>(twMSXConfiguration_MSXServerSelected);
            dbToolWindow.MSXServerUnselected += new EventHandler(dbToolWindow_MSXServerUnselected);
            base.Content = dbToolWindow;
        }

        void dbToolWindow_MSXServerUnselected(object sender, EventArgs e)
        {
            cmdDelete.Enabled = false;
        }

        void twMSXConfiguration_MSXServerSelected(object sender, SelectedServerEventArgs<centraldba_library.Admin.msx_server> e)
        {
            cmdDelete.Enabled = true;
        }

        private void cmdAdd_Click(object sender, EventArgs arguments)
        {
            ((msx_list)base.Content).add_msx_server();
        }

        private void cmdDelete_Click(object sender, EventArgs arguments)
        {
            ((msx_list)base.Content).delete_msx_server();
        }

        private void cmdRefresh_Click(object sender, EventArgs arguments)
        {
            ((msx_list)base.Content).refresh_msx_server();
        }
    }
}
